﻿using sydfdl.TMS.BLL;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;

namespace sydfdl.TMS.Areas.SystemData.Controllers
{
    public class HierarchyTreeController : Controller
    {
        #region 业务逻辑层

        BLLT_B_HierarchyTree bllTree = new BLLT_B_HierarchyTree();

        #endregion


        #region 页面

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加根节点
        /// </summary>
        /// <returns></returns>
        public ActionResult AddRootNode()
        {
            return View();
        }

        /// <summary>
        /// 添加子节点
        /// </summary>
        /// <returns></returns>
        public ActionResult AddLeafNode()
        {
            return View();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 获取层级树所有节点
        /// </summary>
        public void GetAllHierarchyTreeList()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = bllTree.GetAllHierarchyTreeList();

            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }
            string str = MiniUIJson.Encode(dt);

            Response.Write(str);
        }

        /// <summary>
        /// 获取层级树所有可用根节点
        /// </summary>
        public void GetAllHierarchyTreeRoot()
        {
            DataTable dt = new DataTable();

            string key = Request["key"];
            string query = string.Empty;

            if (!string.IsNullOrEmpty(key))
            {
                Dictionary<string, string> list = JsonHelp.JsonToDictionary(key);
                if (list.Count > 0)
                {
                    foreach (var item in list)
                    {
                        switch (item.Key.ToLower())
                        {
                            case "query":
                                {
                                    query = item.Value.ToString();
                                    break;
                                }
                            default:
                                break;
                        }
                    }
                }
            }

            try
            {
                dt = bllTree.GetAllHierarchyTreeRoot(query);
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }

            string str = MiniUIJson.Encode(dt);

            Response.Write(str);
        }

        /// <summary>
        /// 获取层级树子节点
        /// </summary>
        public void GetAllHierarchyTreeLeaf()
        {
            DataTable dt = new DataTable();
            string type = string.Empty;
            string code = string.Empty;
            string value = string.Empty;

            string key = Request["key"];
            string pageIndex = Request["pageIndex"];
            string pageSize = Request["pageSize"];
            string sortField = Request["sortField"];
            string sortOrder = Request["sortOrder"];
            int cnt = 0;

            if (!string.IsNullOrEmpty(key))
            {
                Dictionary<string, string> list = JsonHelp.JsonToDictionary(key);
                if (list.Count > 0)
                {
                    foreach (var item in list)
                    {
                        switch (item.Key.ToLower())
                        {
                            case "type":
                                {
                                    type = item.Value.ToString();
                                    break;
                                }
                            case "code":
                                {
                                    code = item.Value.ToString();
                                    break;
                                }
                            case "value":
                                {
                                    value = item.Value.ToString();
                                    break;
                                }
                            default:
                                break;
                        }
                    }
                }
            }

            try
            {
                dt = bllTree.GetAllHierarchyTreeLeaf(type, code, value, pageIndex, pageSize, sortField, sortOrder, ref cnt);
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }
            Hashtable htList = new Hashtable();
            htList.Add("data", dt);
            htList.Add("total", cnt);

            string str = MiniUIJson.Encode(htList);

            Response.Write(str);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public string SaveData()
        {
            string json = @"[]";

            IEnumerable<T_B_HierarchyTree> add = Request.Form.Get("add") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_HierarchyTree>>(Request.Form.Get("add"));

            IEnumerable<T_B_HierarchyTree> edit = Request.Form.Get("edit") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_HierarchyTree>>(Request.Form.Get("edit"));

            IEnumerable<T_B_HierarchyTree> remove = Request.Form.Get("remove") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_HierarchyTree>>(Request.Form.Get("remove"));

            string str = bllTree.SaveTreeData(add, edit, remove);

            return str;
        }

        #endregion

    }
}
